import java.io.File;
import java.io.IOException;

import javax.servlet.ServletException;

import org.apache.catalina.LifecycleException;
import org.apache.catalina.Wrapper;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.startup.Tomcat;
import org.apache.tomcat.util.descriptor.web.FilterDef;
import org.apache.tomcat.util.descriptor.web.FilterMap;

public class TomcatStarter {
	private static int port = 8089;
    private static String contextPath = "/yes";
 
    public static void start() throws LifecycleException, IOException, ServletException {  
    	
    	Tomcat tomcat = new Tomcat();
        String baseDir = Thread.currentThread().getContextClassLoader().getResource("").getPath();
        System.out.println("baseDir:" + baseDir);
        tomcat.setBaseDir(baseDir);
        tomcat.setPort(port);
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setPort(port);
        tomcat.setConnector(connector);
 
        //src默认为空，在工程下面可以自定义目录，如webapp
		String src = "src\\main\\webapp";
		String path = new File(src).getAbsolutePath();
		System.out.println("webappPath.getAbsolutePath():" + path);
        
		StandardContext context = (StandardContext)tomcat.addWebapp(contextPath, path);
		addWelcomeFile(context);
		addServlet(context);
		addFilter(context);
		addListener(context);
		
//        WebResourceRoot resources = new StandardRoot(context);
//        resources.addPreResources(new DirResourceSet(resources, "/WEB-INF/classes", new File(baseDir).getAbsolutePath(), "/"));
//        context.setResources(resources);
		
        tomcat.enableNaming();
        //手动创建
        //tomcat.getConnector();
        tomcat.start();
        tomcat.getServer().await();
    }
	
    public static void main(String[] args) throws IOException, LifecycleException, ServletException {
        start();
    }
    
    private static void addWelcomeFile(StandardContext context){
    	context.addWelcomeFile("main_pure.jsp");
    }
    
    
    private static void addServlet(StandardContext context){
    	 Wrapper servletWrapper = context.createWrapper();
         servletWrapper.setName("servlet");
         servletWrapper.setServletClass("com.bokesoft.yes.mid.servlet.ServiceDispatch");
         context.addChild(servletWrapper);
         context.addServletMappingDecoded("/servlet", "servlet", false);
     	 context.addServletMappingDecoded("/servlet/*", "servlet", false);
     	 
     	Wrapper attachWrapper = context.createWrapper();
        attachWrapper.setName("attach");
        attachWrapper.setServletClass("com.bokesoft.yes.mid.servlet.AttachmentServlet");
        context.addChild(attachWrapper);
        context.addServletMappingDecoded("/attach", "attach", false);
    	context.addServletMappingDecoded("/attach/*", "attach", false);
    	
     	Wrapper jsWrapper = context.createWrapper();
     	jsWrapper.setName("extendJs");
     	jsWrapper.setServletClass("ExtendJsServlet");
        context.addChild(jsWrapper);
        context.addServletMappingDecoded("/project/extend.js", jsWrapper.getName(), false);
    }
    
    private static void addFilter(StandardContext context){
    	//添加LoginFilter
    	FilterDef loginFilter = new FilterDef();
    	loginFilter.setFilterName("loginFilter");
    	loginFilter.setFilterClass("com.bokesoft.yes.mid.filter.LoginFilter");
    	loginFilter.addInitParameter("encoding", "UTF-8");
    	loginFilter.addInitParameter("loginpage", "/login.jsp");
    	loginFilter.addInitParameter("style", "default");
    	context.addFilterDef(loginFilter);
    	
    	FilterMap loginMaping = new FilterMap();
    	loginMaping.setFilterName("loginFilter");
    	loginMaping.addURLPattern("/main_pure.jsp");
    	loginMaping.addURLPattern("/main_pure_src.jsp");
    	loginMaping.addURLPattern("/");
    	context.addFilterMap(loginMaping);
    	
    	//添加GZIPFilter
    	FilterDef GZIPFilter = new FilterDef();
    	GZIPFilter.setFilterName("GZIPFilter");
    	GZIPFilter.setFilterClass("com.bokesoft.yes.mid.filter.GZIPFilter");
    	context.addFilterDef(GZIPFilter);
    	
    	FilterMap GZIPMaping = new FilterMap();
    	GZIPMaping.setFilterName("GZIPFilter");
    	GZIPMaping.addURLPattern("/*");
    	context.addFilterMap(GZIPMaping);
    }
    
    private static void addListener(StandardContext context){
    	context.addApplicationListener("com.bokesoft.yes.mid.init.ServerStart");
    }
    

}
